﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Collection.Interfaces;
using Collection.Entities;
using MySoft.Data;
using Collection.DbEntities;
using Itfort;

namespace Collection.Services
{
	public class MerchandiseService : IMerchandiseService
	{
		/// <summary>
		/// 数据库插入事件
		/// </summary>
		public event Action<Merchandise> InsertToDatabase;

		/// <summary>
		/// 运行订阅插入事件的方法
		/// </summary>
		/// <param name="merchandise">方法的参数</param>
		private void OnInsert(Merchandise merchandise)
		{
			if (this.InsertToDatabase != null)
				this.InsertToDatabase(merchandise);
		}

		/// <summary>
		/// 数据库更新事件
		/// </summary>
		public event Action<Merchandise> UpdateToDataBase;
		private void OnUpdate(Merchandise merchandise)
		{
			if (this.UpdateToDataBase != null)
				this.UpdateToDataBase(merchandise);
		}

		private DbSession dbSession;

		public MerchandiseService()
		{
			this.dbSession = new DbSession("Taobao");
		}

		/// <summary>
		/// 批量获取商品信息
		/// </summary>
		/// <param name="index">当前页码</param>
		/// <param name="pageSize">页大小</param>
		/// <returns></returns>
		public List<Merchandise> GetMerchandises(Int32 index, Int32 pageSize)
		{
			return this.dbSession.From<_Merchandise>().OrderBy(_Merchandise._.Volume.Desc).GetPage(pageSize).ToList(index).Convert2<Merchandise>();
		}

		/// <summary>
		/// 添加商品信息对象
		/// </summary>
		/// <param name="merchandise">商品信息对象</param>
		public void AddMerchandise(Merchandise merchandise)
		{
			merchandise.AddTime = DateTime.Now;
			merchandise.UpdateTime = DateTime.Now;
			this.dbSession.Save<_Merchandise>(merchandise.Convert2<_Merchandise>());
			this.OnInsert(merchandise);
		}

		/// <summary>
		/// 指定的商品信息是否存在
		/// </summary>
		/// <param name="catalogId">商品分类</param>
		/// <param name="merchandiseId">商品Id</param>
		/// <returns></returns>
		public bool HasMerchandise(long catalogId, long merchandiseId)
		{
			return this.dbSession.Exists<_Merchandise>(_Merchandise._.CatalogId == catalogId && _Merchandise._.Id == merchandiseId);
		}

		/// <summary>
		/// 更新商品数据信息
		/// </summary>
		/// <param name="merchandise">商品信息对象</param>
		public void UpdateMerchandise(Merchandise merchandise)
		{
			var fields = new Field[] { _Merchandise._.ItemLocation, _Merchandise._.ListTime, _Merchandise._.Modified
                                                        , _Merchandise._.Nick, _Merchandise._.Picture, _Merchandise._.Price, _Merchandise._.ProductId
                                                        , _Merchandise._.Properties, _Merchandise._.SellerCreditScore, _Merchandise._.ShopClickUrl
                                                        , _Merchandise._.Title, _Merchandise._.Volume, _Merchandise._.ClickUrl, _Merchandise._.Commission
                                                        , _Merchandise._.CommissionCount, _Merchandise._.CommissionRate, _Merchandise._.CommissionVolume, _Merchandise._.DelistTime
														,_Merchandise._.UpdateTime};
			var values = new Object[] { merchandise.ItemLocation, merchandise.ListTime, merchandise.Modified
                                                            , merchandise.Nick, merchandise.Picture, merchandise.Price, merchandise.ProductId
                                                            , merchandise.Properties, merchandise.SellerCreditScore, merchandise.ShopClickUrl
                                                            , merchandise.Title, merchandise.Volume, merchandise.ClickUrl, merchandise.Commission
                                                            , merchandise.CommissionCount, merchandise.CommissionRate, merchandise.CommissionVolume, merchandise.DelistTime 
															,DateTime.Now};
			var condition = _Merchandise._.Id == merchandise.Id && _Merchandise._.CatalogId == merchandise.CatalogId;
			this.dbSession.Update<_Merchandise>(fields, values, condition);
			this.OnUpdate(merchandise);
		}

		/// <summary>
		/// 获取产品的数量
		/// </summary>
		public Int64 Count
		{
			get
			{
				return this.dbSession.Count<_Merchandise>(_Merchandise._.Id > 0);
			}
		}
	}
}
